//
// Author: Vladimir Migashko <migashko@gmail.com>, (C) 2011
//
// Copyright: See COPYING file that comes with this distribution
//

#include "hit.hpp"
#include "hitlist.hpp"
//#include "hit_container.hpp"
#include "hit_container.hpp"

hitlist::~hitlist()
{
  delete _hitlist;
  //delete _backup;
}


hitlist::hitlist()
  : _hitlist(new hit_container)
  , _backup(0)
  , _test_mode(false)
{
}

void hitlist::initialize(time_t hit_interval, time_t prepared_timelife, time_t days_in_month, time_t idle_timeout, size_t idle_count)
{
  _hitlist->initialize(hit_interval, prepared_timelife, days_in_month, idle_timeout, idle_count);
}

void hitlist::backup(ibackup* b)
{
  _backup = b;
}

void hitlist::test_mode(bool enable)
{
  _test_mode = enable;
}

size_t hitlist::hits_size() const 
{
   return _hitlist->hits_size();
}

size_t hitlist::hosts_size() const 
{
   return _hitlist->hosts_size();
}

bool hitlist::hit( user_id_t first_id,  user_id_t second_id)
{
  time_t hit_time = time(0);
  if ( _hitlist->hit(first_id,  second_id, hit_time) )
  {
    if ( _backup )
      _backup->write_hit(first_id, second_id, hit_time);
    return true;
  }
  return false;
}

bool hitlist::hit( user_id_t first_id,  user_id_t  second_id, time_t hit_time)
{
  /*::hit h;
  h.first_id = first_id;
  h.second_id = second_id;
  h.hit_time = hit_time;
  */
  if ( _hitlist->hit(first_id,  second_id, hit_time) )
  {
    // тестовый варант
    if ( _test_mode && _backup )
      _backup->write_hit(first_id, second_id, hit_time);
    return true;
  }
  return false;
}

size_t hitlist::hits_day( user_id_t first_id) const
{
  return _hitlist->hits_day(first_id);
}

size_t hitlist::hosts_day( user_id_t first_id) const
{
  return _hitlist->hosts_day(first_id);
}

size_t hitlist::hits_month( user_id_t first_id) const
{
  return _hitlist->hits_month(first_id);
}

size_t hitlist::hosts_month( user_id_t first_id) const
{
  return _hitlist->hosts_month(first_id);
}

void hitlist::day_hitlist(  user_id_t first_id, size_t offset, size_t limit, std::vector<hit_item>& hl ) const
{
  _hitlist->day_hitlist(first_id, offset, limit, hl );
}

void hitlist::month_hitlist(  user_id_t first_id, size_t offset, size_t limit, std::vector<hit_item>& hl ) const
{
  _hitlist->month_hitlist(first_id, offset, limit, hl );
}

void hitlist::clear()
{
  _hitlist->clear();
}

std::pair<size_t, size_t> hitlist::idle()
{
  return _hitlist->idle();
}

